package com.itheima.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.vo.CommentVo;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class UserManager {

    @DubboReference
    UserInfoService userInfoService;

    @DubboReference
    MovementService movementService;

    @DubboReference
    CommentService commentService;

    // 用户列表
    public ResponseEntity findUserInfoByPage(Integer pageNum, Integer pageSize) {
        // 1.rpc查询
        PageBeanVo pageBeanVo = userInfoService.findByPage(pageNum, pageSize);
        // 2.返回
        return ResponseEntity.ok(pageBeanVo);
    }

    // 用户详情
    public ResponseEntity findById(Long userId) {
        UserInfo userInfo = userInfoService.findById(userId);
        return ResponseEntity.ok(userInfo);
    }

    public ResponseEntity findMovementByConditionAndPage(Long userId, Integer state, Integer pageNum, Integer pageSize) {
        //1.若userId不为空,查询userInfo
        UserInfo userInfo = null;
        if (userId != null) {
            userInfo = userInfoService.findById(userId);
        }

        //2.远程调用service查询当前页数据
        PageBeanVo pageBeanVo = movementService.findByConditionAndPage(userId, state, pageNum, pageSize);

        //3.获取分页中items(movement集合),遍历集合,当userId为空时获取发布者UserInfo,将movement和userInfo封装成MovementVo,将vo放入voList中
        List<MovementVo> voList = new ArrayList<>();

        List<Movement> items = (List<Movement>) pageBeanVo.getItems();

        if (CollUtil.isNotEmpty(items)) {
            for (Movement movement : items) {
                //当userId为空时获取发布者UserInfo
                if (userId == null) {
                    userInfo = userInfoService.findById(movement.getUserId());
                }

                //将movement和userInfo封装成MovementVo
                MovementVo vo = new MovementVo();

                //先封装userInfo,再封装movement
                vo.setUserInfo(userInfo);
                vo.setMovement(movement);

                //单独处理下时间
                vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));

                //将vo放入voList中
                voList.add(vo);
            }
        }

        //4.将voList设置给pageBeanVo
        pageBeanVo.setItems(voList);
        //5.返回pageBeanVo
        return ResponseEntity.ok(pageBeanVo);
    }

    // 单条动态
    public ResponseEntity findMovementById(ObjectId publishId) {
        //1.远程调用service查询动态对象
        Movement movement = movementService.findById(publishId);

        //2.远程调用service查询发布人对象
        UserInfo userInfo = userInfoService.findById(movement.getUserId());

        //3.封装MovementVo对象且返回
        MovementVo vo = new MovementVo();
        //先封装userInfo,再封装movement
        vo.setUserInfo(userInfo);
        vo.setMovement(movement);
        //单独处理下时间
        vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));
        return ResponseEntity.ok(vo);
    }

    // 动态评论列表
    public ResponseEntity findMovementComemntByPage(Integer pageNum, Integer pageSize, ObjectId publishId) {
        //1.远程调用service查询评论的分页数据
        PageBeanVo pageBeanVo = commentService.findByPage(publishId, pageNum, pageSize);

        //2.获取分页中的items(comment),遍历集合,获取每条评论的发布人的userInfo,将useInfo和comment封装CommentVo对象,将vo对象放入voList中
        List<CommentVo> voList = new ArrayList<>();

        List<Comment> items = (List<Comment>) pageBeanVo.getItems();
        if (CollUtil.isNotEmpty(items)) {
            items.forEach(c->{
                // 获取每条评论的发布人的userInfo,
                UserInfo userInfo = userInfoService.findById(c.getUserId());

                // 将useInfo和comment封装CommentVo对象,
                CommentVo vo = new CommentVo();
                vo.setId(c.getId().toString());
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());
                vo.setContent(c.getContent());
                vo.setCreateDate(DateUtil.formatDateTime(new Date(c.getCreated())));

                // 将vo对象放入voList中
                voList.add(vo);
            });
        }

        //3.将voList设置给pageBeanVo
        pageBeanVo.setItems(voList);
        //4.返回pageBeanVo
        return ResponseEntity.ok(pageBeanVo);
    }
}
